package com.sdy.hospitalguard.h5.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sdy.hospitalguard.biz.model.VisitAppointment;
import com.sdy.hospitalguard.biz.service.VisitAppointmentService;
import com.sdy.mvc.annotation.TaskSync;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 注释
 * </p>
 *
 * @author fb
 * @since 2020/5/29
 */
@Component
@Slf4j
public class VisitTask {

    @Autowired
    private VisitAppointmentService visitAppointmentService;

    /**
     * 产生探视过期记录，实时性要求高
     */
    @Scheduled(cron = "0/5 * * * * ?")
    @TaskSync
    public void processVisitTime() {
        Date date = new Date();
        QueryWrapper<VisitAppointment> wrapper = new QueryWrapper<>();
        wrapper.lt("visit_end_time", date);
        wrapper.eq("visit_state", 0);
        wrapper.isNull("practical_start_time");
        wrapper.isNull("practical_end_time");
        List<VisitAppointment> list = visitAppointmentService.list(wrapper);
        for (int i=0; i< list.size(); i++) {
            visitAppointmentService.update(new UpdateWrapper<VisitAppointment>()
                    .eq("id", list.get(i).getId())
                    .set("visit_state", 3));
        }
    }

}
